<?php

/* * 
 * 活动参与
 */
namespace Api\Model;
use Think\Model;

class ActivityJoinModel extends Model {

    //映射
    protected $_map = array(
        'activityId' => 'activity_id',
    );

    //获取活动记录
    public function getActivityJoinById($id) {

        $activityJoin = $this->where(array('join_id' => $id))->find();

        return $activityJoin;

    }

    //添加活动报名记录
    public function addActivityJoin($userId) {

        //校验字段
        $validateRules = array(
            array('activity_id', 'require'),
        );

        //自动完成
        $autoRules = array(
            array('create_time', 'getCurrentDate',self::MODEL_INSERT,'function'),
            array('user_id', $userId)
        );

        $activityJoin = $this->validate($validateRules)->auto($autoRules)->create();

        if(!$activityJoin) {
            return 'ERROR_NECESSARY_DATA_NOT_FOUND';
        } else {

            $activityRecord = D('activity')->getActivityById($activityJoin['activity_id']);
            if(isNullOrEmpty($activityRecord)) {
                return "ERROR_ACTIVITY_NOT_FOUND";
            }

            if($activityRecord['status'] == C("ACTIVITY_STATUS.VERIFY") || $activityRecord['status'] == C("ACTIVITY_STATUS.END")) {
                return "ERROR_ACTIVITY_VERIFY_OR_END";
            }

            if($this->checkActivityJoin($activityJoin['activity_id'],$userId)) {
                return "ERROR_USER_ALREADY_JOIN_ACTIVITY";
            }

            $this->add();

            return "SUCCESS";
        }

    }

    //判断是否报名活动
    public function checkActivityJoin($activityId, $userId) {

        $map['activity_id'] = $activityId;
        $map['user_id'] = $userId;
        $activityJoin = $this->where($map)->find();

        if(!isNullOrEmpty($activityJoin)) {
            return true;
        } else {
            return false;
        }

    }

    //根据用户ID获取活动报名列表
    public function getActivityJoinListByUserId($userId, $status, $pageNum) {

        $map['aj.user_id'] = $userId;
        $map['a.status'] = $status;

        $list = $this->alias('aj')
            ->join("LEFT JOIN __ACTIVITY__ a ON aj.activity_id = a.activity_id")
            ->field("a.activity_id, a.activity_name, a.activity_image_url, a.time_string, a.address, a.join_count, a.collect_count")
            ->where($map)
            ->order("aj.create_time ASC")
            ->limit(($pageNum-1)*C('DEFAULT_PAGE_SIZE') . ',' . C('DEFAULT_PAGE_SIZE'))
            ->select();

        //获取每个活动的多个分类
        foreach ($list as &$item) {
            $item['category_list'] = D('categoryRelationship')->getCategoryList($item['activity_id'],C("CATEGORY_RELATIONSHIP_TYPE.ACTIVITY"));
        }

        return $list;

    }

    //获取其他用户活动列表
    public function getUserActivityJoinList($userId, $pageNum) {

        $map['aj.user_id'] = $userId;

        $list = $this->alias('aj')
            ->join("LEFT JOIN __ACTIVITY__ a ON aj.activity_id = a.activity_id")
            ->field("a.activity_id, a.activity_name, a.activity_image_url, a.time_string")
            ->where($map)
            ->order("aj.create_time ASC")
            ->limit(($pageNum-1)*C('DEFAULT_PAGE_SIZE') . ',' . C('DEFAULT_PAGE_SIZE'))
            ->select();

        return $list;

    }

    //获取报名人数
    public function getActivityJoinCountByActivityId($activityId) {

        $map['activity_id'] = $activityId;
        $count = $this->where($map)->count();

        return $count;

    }

    //获取活动报名列表（限5个, 仅显示头像）
    public function getActivityJoinListBasicByActivityId($activityId) {

        $map['aj.activity_id'] = $activityId;
        $list = $this->alias('aj')
            ->join("LEFT JOIN __USER__ u ON aj.user_id = u.user_id")
            ->field("u.user_id, u.head_image_url")
            ->where($map)
            ->order("aj.create_time ASC")
            ->limit(5)
            ->select();

        return $list;

    }

    //获取完整活动列表
    public function getActivityJoinListByActivityId($activityId, $pageNum, $userId) {

        $map['aj.activity_id'] = $activityId;
        $list = $this->alias('aj')
            ->join("LEFT JOIN __USER__ u ON aj.user_id = u.user_id")
            ->field("u.user_id, u.username, u.head_image_url, u.city")
            ->where($map)
            ->order("aj.create_time ASC")
            ->limit(($pageNum-1)*C('DEFAULT_PAGE_SIZE') . ',' . C('DEFAULT_PAGE_SIZE'))
            ->select();

        foreach ($list as &$item) {
            //判断是否为好友
            $item['is_friend'] = D('friend')->checkFriendRelationship($userId,$item['user_id']);
        }

        return $list;

    }


}